package com.gnerv.battle.boot.framework.core.tools;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * AES 加解密
 *
 * @author Gnerv LiGen
 */
public class AesTools {

    /**
     * 算法
     */
    private static final String CODE_MODE = "AES";

    /**
     * 默认密钥 32字节 必须为16字节、24字节或32字节
     */
    private static final String KEY = "AE241F62C5F84F84A6CE7A305FC78151";
    private static final byte[] KEYS = KEY.getBytes(StandardCharsets.UTF_8);
    private static final GCMParameterSpec CIPHER = new GCMParameterSpec(128, KEYS);

    /**
     * "算法/模式/补码方式"
     */
    private static final String TRANS_FORMATION = "AES/GCM/NoPadding";

    private AesTools() {
    }

    public static String encrypt(String content) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
        return encrypt(content, KEYS);
    }

    public static String encrypt(String content, byte[] keys) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
        SecretKey secretKey = new SecretKeySpec(keys, CODE_MODE);
        Cipher cipher = Cipher.getInstance(TRANS_FORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, CIPHER);
        return Base64.getEncoder().encodeToString(cipher.doFinal(content.getBytes(StandardCharsets.UTF_8)));
    }

    public static String decrypt(String content) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
        return decrypt(content, KEYS);
    }

    public static String decrypt(String content, byte[] keys) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
        // 将密文转换为16字节的字节数组
        byte[] contentBytes = Base64.getDecoder().decode(content);
        SecretKey secretKey = new SecretKeySpec(keys, CODE_MODE);
        Cipher cipher = Cipher.getInstance(TRANS_FORMATION);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, CIPHER);
        return new String(cipher.doFinal(contentBytes), StandardCharsets.UTF_8);
    }


    public static void main(String[] args) throws NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
        String decrypt = decrypt("m6RsDfnrxnKx5FtQbQDtrcO9t9Kr8NhiGMagTd87T0Wxxtm1p05m3mZ5fFGEeMVPVspYWcbgA0Ju3h/Vh1jbYNiN1ggZ2MLNSIFeEEMcPbmFTLKD24teeNIwic/BJ7X9d9YjxOzPvzRLmbdJ6DsIoEGOrREAausnmb2Xl1lefl+4/PmdBbOUxsH8G2wKVaVJlm7uGsE=",
                "ae162a9342be4759bdf394afc46c955a".getBytes(StandardCharsets.UTF_8));
        System.out.println(decrypt);
    }
}
